package com.yoomiito.app.ui.anyview.shareview

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Bitmap
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.yoomiito.app.R
import com.yoomiito.app.model.bean.GoodsDetail
import com.yoomiito.app.utils.AllCode
import com.yoomiito.app.utils.ImageUtils
import com.yoomiito.app.utils.L
import com.yoomiito.app.utils.SpannableUtils
import com.yoomiito.app.widget.ImageView1_1

/**
 * 自营商品  爆品 优品 礼包的商详分享
 */
class SharePrivateGoodsView(val context: Context) {
    private val view = LayoutInflater.from(context).inflate(R.layout.gift_share_pic_1, null)
    private val mGoodsPic = view.findViewById<ImageView1_1>(R.id.gift_share_pic_goods_iv)
    private val erQRCodeIv = view.findViewById<ImageView>(R.id.gift_share_pic_erweima)
    private val mGoodsTitleTv = view.findViewById<TextView>(R.id.gift_share_pic_goods_title)
    private val mGoodsPriceTv = view.findViewById<TextView>(R.id.gift_share_pic_goods_price)
    private val mGoodsOldPriceTv = view.findViewById<TextView>(R.id.gift_share_pic_goods_old_price)
    private val mGoodsSaleSizeTv = view.findViewById<TextView>(R.id.gift_share_pic_sale_size)
    private var mGoodsPicLoadSuccess = false
    private var mQRCodeLoadSuccess = false
    var onPicMakeListener:(Bitmap)->Unit={}
    @SuppressLint("SetTextI18n")
    fun initView(goodsDetail: GoodsDetail){
        val corners = RoundedCorners(12)
        val options = RequestOptions.bitmapTransform(corners)
        Glide.with(context).asBitmap().apply(options).load(goodsDetail.pict_url).into(object : SimpleTarget<Bitmap>() {
            override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                mGoodsPic.setImageBitmap(resource)
                mGoodsPicLoadSuccess = true
                makePic()
            }
        })
        mGoodsTitleTv.text = goodsDetail.title
        mGoodsPriceTv.text = SpannableUtils.getDiscountPrice("¥" + SpannableUtils.handlePrice(goodsDetail.salePrice) + "专享", R.color.color_balk_333333)
        val marketPrice = goodsDetail.reserve_price
        mGoodsOldPriceTv.text = SpannableUtils.getSpannable("原价" + SpannableUtils.handlePrice(marketPrice), 0)//goodsShareInfo.getReserve_price()
        if (SpannableUtils.isEmptyOr0(marketPrice)) {
            mGoodsOldPriceTv.visibility = View.GONE
        }
        mGoodsSaleSizeTv.text = "销量" + SpannableUtils.saleNum(goodsDetail.volume.toDouble())
    }

    fun setQRCodeUrl(url:String){
        Glide.with(context).asBitmap().load(url).into(object : SimpleTarget<Bitmap>() {
            override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                erQRCodeIv.setImageBitmap(resource)
                mQRCodeLoadSuccess = true
                makePic()
            }
        })
    }

    private fun makePic(){
        ImageUtils.layoutView(view)
        if (mQRCodeLoadSuccess&&mGoodsPicLoadSuccess) onPicMakeListener(ImageUtils.viewSaveToImage(view))
    }

}